from moodlefuse.parsers import Parser
from files import Assignment, AssignmentFile
from moodlefuse.files import FileSource
import re

class AssigmentParser(Parser):

    def parse_assignment(self):
        soup = self.get_parsed_data()

        instructions    = soup.find('div', attrs={'id':'intro'}).find('p').text.encode('utf-8')
        start_date      = None
        end_date        = None

        return instructions, start_date, end_date

class AssignmentFilesParser(Parser):

    def parse_assignment_files(self):
        soup = self.get_parsed_data()

        files = {}
        files_container_html = soup.find('div', {'class': 'files'})
        if not files_container_html:
            return files

        files_html = files_container_html.findAll('a')
        for file_html in files_html:
            name    = self.parse_file_name(file_html)
            url     = self.parse_file_url(file_html)

            file = AssignmentFile(self.api, name, url, assignment_url=self.url, source=FileSource.DOWNLOADED)
            files[file.name] = file

        return files

    def parse_file_name(self, html):
        return html.text.encode('utf-8')

    def parse_file_url(self, html):
        return html['href']    

class AssignmentsParser(Parser):

    def parse_assignments(self):
        soup = self.get_parsed_data()
        assignments_html = soup.findAll('a', attrs={'href':re.compile('assignment/view\.php\?id=\d+')})

        assignments = {}
        for assignment_html in assignments_html:
            name    = self.parse_assignment_name(assignment_html)
            url     = self.parse_assignment_url(assignment_html)
            
            #assignment_parser = AssigmentParser(self.api, self.browser, url)
            #instructions, start_date, end_date = assignment_parser.parse_assignment()

            assignment = Assignment(self.api, name, url)
            assignments[assignment.name] = assignment

        return assignments

    def parse_assignment_name(self, html):
        return html.text.encode('utf-8')

    def parse_assignment_url(self, html):
        return html['href']


        

  